package com.framework.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fhs.trans.service.impl.DictionaryTransService;
import com.framework.core.entity.SysDict;
import com.framework.core.entity.SysDictType;
import com.framework.core.service.SysDictService;
import com.framework.core.service.SysDictTypeService;
import com.framework.mybatis.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 加载数据字典
 *
 * @author lihanbo
 * @date 2021/6/21 11:17
 */
@Service("coreSysDictService")
@Slf4j
public class SysDictServiceImpl extends BaseServiceImpl<SysDict> implements SysDictService {

    //注入字典翻译服务
    @Autowired
    private DictionaryTransService dictionaryTransService;
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @Override
    public void initDictTrans() {
        List<SysDictType> list = sysDictTypeService.list(new QueryWrapper<>());
        List<SysDict> dictList = this.list(new QueryWrapper<>());
        list.forEach(x -> {
            List<SysDict> collect = dictList.stream().filter(item -> item.getTypeId().equals(x.getId())).collect(Collectors.toList());
            Map<String, String> transMap = new HashMap<>();
            collect.forEach(item -> {
                transMap.put(item.getDictCode(), item.getDictText());
            });
            dictionaryTransService.refreshCache(x.getTypeCode(), transMap);
        });
    }


}
